<?php 
/** Валидатор проверяет, содержится ли значение поля $needleField в значении поля $haystackField
  *
  *
  */
  

class sfValidatorShemaContains extends sfValidatorSchema
{
  public function __construct($needleField, $haystackField, $messages = array())
  {
    $this->addOption('neeldle_field', $needleField);
    $this->addOption('haystack_field', $haystackField);
    $this->addOption('throw_global_error', false);

    parent::__construct(null, array(), $messages);
  }
  
  protected function doClean($values)
  {
    if (null === $values)
    {
      $values = array();
    }
    
    if (!is_array($values))
    {
      throw new InvalidArgumentException('You must pass an array parameter to the clean() method');
    }
    
    
    $needleValue  = isset($values[$this->getOption('neeldle_field')]) ? $values[$this->getOption('neeldle_field')] : null;
    $haystackValue = isset($values[$this->getOption('haystack_field')]) ? $values[$this->getOption('haystack_field')] : null;
    
    if (!is_array($haystackValue))
      $haystackValue=array($haystackValue);
    
    $valid = $this->isValid($needleValue,$haystackValue);
    
    if (!$valid)
    {
      $error = new sfValidatorError($this, 'invalid', array(
        'neelde_field'  => $needleValue,
        'haystack_field' => $haystackValue,
      ));
      if ($this->getOption('throw_global_error'))
      {
        throw $error;
      }

      throw new sfValidatorErrorSchema($this, array($this->getOption('neelde_field') => $error));
    }

    return $values;
  }
  
  protected function isValid($needleValue,$haystackValue)
  {
    return in_array($needleValue,$haystackValue) ? true:false;  
  }
}